global root_dir = "`1'"

include "$root_dir/code/config/config.do"


cap noi log using ${log_dir}/build_patlevel_classification_stats.log, replace name(dat)

*Handle empty arguments
global arg1 = cond("`2'" == "___EMPTY___", "", "`2'")
global arg2 = cond("`3'" == "___EMPTY___", "", "`3'")
global arg3 = cond("`4'" == "___EMPTY___", "", "`4'")
global arg4 = cond("`5'" == "___EMPTY___", "", "`5'")

if "$arg1" != "" {
    global weight_category "$arg1"
    di "Weight category: ${weight_category}"
}

if "$arg2" != "" {
    global weight_versions "$arg2"
    di "Weight versions: ${weight_versions}"
}

if "$arg3" != "" {
    global weight_window "$arg3"
    di "Weight window: ${weight_window}"
}

if "$arg4" != "" {
	global wtype "$arg4"
}
di "${wtype}"
capture noi {

**************************************************************************************************************************
* Create comprehensive patent level classification descriptive dataset for biadic patent families in the year 1997 - 2011*
**************************************************************************************************************************

*load patents and initial filing years
use appln_id docdb_family_id fam_earliest_appln_year using ${commondata_dir}/patstat_2018b/family_info.dta, clear

*laod biadic information
mmerge docdb_family_id using ${dataset_dir}/patstat_orbis/docdb_families2.dta, unmatched(master) ukeep(biadic_D)
keep if fam_earliest_appln_year >= 1997 & fam_earliest_appln_year <= 2011
keep if biadic_D
drop biadic_D

*restrict to correct intelectual property type (type 2 = patents)
mmerge appln_id using ${commondata_dir}/patstat_2018b/appln_info.dta, unmatched(master) ukeep(ipr_type)
keep if ipr_type == "PI":ipr_type2

* identify auto80/90/95
foreach q in 80 90 95 {
    mmerge appln_id using ${dataset_dir}/patent_list/auto`q'_patents.dta, unmatched(master)
    gen isauto`q' = _m == 3
}

* identify 90/95 by technology class (ipc4, ipc6xx, ipc4_pairs)
foreach q in 90 95 {
    foreach class in ipc4 ipc6XX ipc4_pairs {
        mmerge appln_id using ${dataset_dir}/patent_list/auto`q'_`class'_patents.dta, unmatched(master)
        gen isauto`q'_`class' = _m == 3
    }
}

* identify the "subtypes" comp keywords (robo80/90/95, CNC80/90/95, autm80/90/95)
foreach q in 80 90 95 {
    foreach comp in robo CNC autm {
        mmerge appln_id using ${dataset_dir}/patent_list/`comp'`q'_patents.dta, unmatched(master)
        gen is`comp'`q' = _m == 3
    }
}

* identify auto95 patents by different classification periods
preserve
cap program drop smpl
program smpl, rclass
    args a
    local N0 "V6"
    local N1 "V6-1997-2011"
    local N2 "V6-until-1997"
    local N3 "V6-from-1998"
    return local name `N`a''
end

*import the classification lists for each classfication period and make them into dta files
foreach sample in 0 1 2 3 {
    smpl `sample'
    import delimited using ${classification_dir}/`r(name)'/patent_lists/automation95.csv, clear
    tempfile f`sample'
    save `f`sample''
}
restore

*mark the auto95 patents in the dataset
foreach sample in 0 1 2 3 {
    mmerge docdb_family_id using `f`sample'', unmatched(master)
    gen isauto95_`sample' = (_m == 3)
    drop _m
}


duplicates drop docdb_family_id, force
drop appln_id ipr_type
save ${final_dir}/patlevel_classification_stats.dta, replace



}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close dat